/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.sys.service.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUserProduct;
import com.thinkgem.jeesite.modules.hk.sys.dao.user.HkUserProductDao;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUserProductCategory;
import com.thinkgem.jeesite.modules.hk.sys.dao.user.HkUserProductCategoryDao;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUserProductImg;
import com.thinkgem.jeesite.modules.hk.sys.dao.user.HkUserProductImgDao;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUserProductSku;
import com.thinkgem.jeesite.modules.hk.sys.dao.user.HkUserProductSkuDao;

/**
 * 用户产品Service
 * @author 罗天文
 * @version 2018-08-30
 */
@Service
@Transactional(readOnly = true)
public class HkUserProductService extends CrudService<HkUserProductDao, HkUserProduct> {

	@Autowired
	private HkUserProductCategoryDao hkUserProductCategoryDao;
	@Autowired
	private HkUserProductImgDao hkUserProductImgDao;
	@Autowired
	private HkUserProductSkuDao hkUserProductSkuDao;
	
	public HkUserProduct get(String id) {
		HkUserProduct hkUserProduct = super.get(id);
		hkUserProduct.setHkUserProductCategoryList(hkUserProductCategoryDao.findList(new HkUserProductCategory(hkUserProduct)));
		hkUserProduct.setHkUserProductImgList(hkUserProductImgDao.findList(new HkUserProductImg(hkUserProduct)));
		hkUserProduct.setHkUserProductSkuList(hkUserProductSkuDao.findList(new HkUserProductSku(hkUserProduct)));
		return hkUserProduct;
	}
	
	public List<HkUserProduct> findList(HkUserProduct hkUserProduct) {
		return super.findList(hkUserProduct);
	}
	
	public Page<HkUserProduct> findPage(Page<HkUserProduct> page, HkUserProduct hkUserProduct) {
		return super.findPage(page, hkUserProduct);
	}
	
	@Transactional(readOnly = false)
	public void save(HkUserProduct hkUserProduct) {
		super.save(hkUserProduct);
		for (HkUserProductCategory hkUserProductCategory : hkUserProduct.getHkUserProductCategoryList()){
			if (hkUserProductCategory.getId() == null){
				continue;
			}
			if (HkUserProductCategory.DEL_FLAG_NORMAL.equals(hkUserProductCategory.getDelFlag())){
				if (StringUtils.isBlank(hkUserProductCategory.getId())){
					hkUserProductCategory.setProduct(hkUserProduct);
					hkUserProductCategory.preInsert();
					hkUserProductCategoryDao.insert(hkUserProductCategory);
				}else{
					hkUserProductCategory.preUpdate();
					hkUserProductCategoryDao.update(hkUserProductCategory);
				}
			}else{
				hkUserProductCategoryDao.delete(hkUserProductCategory);
			}
		}
		for (HkUserProductImg hkUserProductImg : hkUserProduct.getHkUserProductImgList()){
			if (hkUserProductImg.getId() == null){
				continue;
			}
			if (HkUserProductImg.DEL_FLAG_NORMAL.equals(hkUserProductImg.getDelFlag())){
				if (StringUtils.isBlank(hkUserProductImg.getId())){
					hkUserProductImg.setProduct(hkUserProduct);
					hkUserProductImg.preInsert();
					hkUserProductImgDao.insert(hkUserProductImg);
				}else{
					hkUserProductImg.preUpdate();
					hkUserProductImgDao.update(hkUserProductImg);
				}
			}else{
				hkUserProductImgDao.delete(hkUserProductImg);
			}
		}
		for (HkUserProductSku hkUserProductSku : hkUserProduct.getHkUserProductSkuList()){
			if (hkUserProductSku.getId() == null){
				continue;
			}
			if (HkUserProductSku.DEL_FLAG_NORMAL.equals(hkUserProductSku.getDelFlag())){
				if (StringUtils.isBlank(hkUserProductSku.getId())){
					hkUserProductSku.setProduct(hkUserProduct);
					hkUserProductSku.preInsert();
					hkUserProductSkuDao.insert(hkUserProductSku);
				}else{
					hkUserProductSku.preUpdate();
					hkUserProductSkuDao.update(hkUserProductSku);
				}
			}else{
				hkUserProductSkuDao.delete(hkUserProductSku);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkUserProduct hkUserProduct) {
		super.delete(hkUserProduct);
		hkUserProductCategoryDao.delete(new HkUserProductCategory(hkUserProduct));
		hkUserProductImgDao.delete(new HkUserProductImg(hkUserProduct));
		hkUserProductSkuDao.delete(new HkUserProductSku(hkUserProduct));
	}
	
}